home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / Programming / imageio.lib / dev / examples / write / main.c next >
Encoding:
C/C++ Source or Header  |  2000-08-09  |  3.6 KB  |  150 lines

  1. /* This example use of imageio.library loads the image file specified
  2.         on the command line at half size and saves it in jpeg format, first
  3.         querying if it is indeed possible to do so. The file is saved in
  4.         'ram:test'.
  5. */
  6.  
  7. #include <stdio.h>
  8.  
  9. #include <dos/dos.h>
  10. #include <exec/memory.h>
  11. #include <exec/types.h>
  12.  
  13. #include <clib/dos_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/intuition_protos.h>
  16.  
  17. #include <pragmas/dos_pragmas.h>
  18. #include <pragmas/exec_pragmas.h>
  19. #include <pragmas/intuition_pragmas.h>
  20.  
  21. #include <imageio/imageio.h>
  22. #include <imageio/imageio_protos.h>
  23. #include <imageio/imageio_pragmas.h>
  24.  
  25. /* Function prototypes */
  26. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata );
  27.  
  28. extern struct Library *DOSBase;
  29. struct Library *ImageIOBase, *IntuitionBase;
  30.  
  31. void main( int argc, char **argv )
  32. {
  33.     if ( argv[1] != NULL )
  34.     {
  35.         ImageIOBase = OpenLibrary( "imageio.library", 2 );
  36.         IntuitionBase = OpenLibrary( "intuition.library", NULL );
  37.         if ( IntuitionBase && ImageIOBase )
  38.         {
  39.             struct ImageHandle *ih;
  40.             ULONG err;
  41.             BPTR fp;
  42.  
  43.             fp = Open( argv[1], MODE_OLDFILE );
  44.             if ( fp != NULL )
  45.             {
  46.                 err = AllocImage( &ih, IMG_SrcFile, fp, TAG_DONE );
  47.                 if ( !err )
  48.                 {
  49.                     ULONG num = 1, denom = 2;
  50.                     ULONG x, y, bpp, rs;
  51.                     UBYTE *buffer, cs, it;
  52.                     UBYTE saveable;
  53.  
  54.                     err = GetImageAttrs( ih,
  55.                         IMG_Width, &x,
  56.                         IMG_Height,&y,
  57.                         IMG_BytesPerPixel, &bpp,
  58.                         IMG_ColourSpace, &cs,
  59.                         IMG_RowSize, &rs,
  60.                         IMG_TestScaleNum, num,
  61.                         IMG_TestScaleDenom, denom,
  62.                         TAG_DONE );
  63.                     if ( !err )
  64.                     {
  65.                         printf( "width=%ld, height=%ld\n", x, y );
  66.                         printf( "bytes per pixel=%ld, colourspace=%d\n", bpp, cs );
  67.                         printf( "row size=%ld\n", rs );
  68.  
  69.                         err = ReadImage( ih,
  70.                             IMG_ScaleNum, num,
  71.                             IMG_ScaleDenom, denom,
  72.                             IMG_ImageBuffer, &buffer,
  73.                             IMG_ProgressHook, progressFunc,
  74.                             TAG_DONE );
  75.                         if ( !err )
  76.                         {
  77.                             err = GetImageAttrs( ih,
  78.                                 IMG_ImageType, &it,
  79.                                 TAG_DONE );
  80.                             if ( !err )
  81.                             {
  82.                                 printf("Image type=%d\n",it);
  83.                             }
  84.  
  85.                             saveable = IMGF_ILBM;
  86.                             err = GetImageAttrs( ih,
  87.                                 IMG_QuerySaveable, &saveable,
  88.                                 TAG_DONE );
  89.                             if ( !err )
  90.                             {
  91.                                 printf( "can save in ILBM: %s\n", saveable == 1 ? "Yes" : "No" );
  92.                             }
  93.  
  94.                             saveable = IMGF_JPEG;
  95.                             err = GetImageAttrs( ih,
  96.                                 IMG_QuerySaveable, &saveable,
  97.                                 TAG_DONE );
  98.                             if ( !err )
  99.                             {
  100.                                 printf( "can save in JPEG: %s\n", saveable == 1 ? "Yes" : "No" );
  101.                             }
  102.  
  103.                             if ( saveable == 1 )
  104.                             {
  105.                                 err = WriteImage( ih, IMGF_JPEG, "ram:test",
  106.                                     IMG_ProgressHook, progressFunc,
  107.                                     IMG_JPEG_Quality, 90,
  108.                                     TAG_DONE );
  109.                                 if ( !err )
  110.                                 {
  111.                                     printf("Image saved\n");
  112.                                 }
  113.                                 else printf( "write image error:%d\n", err );
  114.                             }
  115.                         }
  116.                         else printf( "read image error:%d\n", err );
  117.                     }
  118.                     else printf( "get image attrs error:%d\n", err );
  119.  
  120.                     FreeImage( ih );
  121.                 }
  122.                 else printf( "alloc image error:%d\n", err );
  123.  
  124.                 Close( fp );
  125.             }
  126.             else printf( "cant open file\n" );
  127.         }
  128.  
  129.         if ( ImageIOBase ) CloseLibrary( ImageIOBase );
  130.         if ( IntuitionBase ) CloseLibrary ( IntuitionBase );
  131.     }
  132.     else printf( "no file specified\n" );
  133. }
  134.  
  135. __saveds __asm ULONG progressFunc( register __d0 ULONG curr, register __d1 ULONG lines, register __a0 void *userdata )
  136. {
  137.     static int prevpercent = 0;
  138.  
  139.     int percent = ( curr * 100 ) / lines;
  140.  
  141.     if ( prevpercent != percent )
  142.     {
  143.         if ( percent % 10 == 0 ) printf( "%d%%\n", percent );
  144.     }
  145.  
  146.     prevpercent = percent;
  147.  
  148.     return NULL;
  149. }
  150.